using System;
using System.Collections.Generic;
using System.Text;
using TrainTicket.DAO;
using System.Data;
using TrainTicket.VO;

namespace TrainTicket.TRAIN
{
    public class AccountTRAIN
    {
        private AccountDAO _accountDAO;
        public AccountTRAIN()
        {
            _accountDAO = new AccountDAO();
        }

        public bool CheckAccount(string userName, string password)
        {
            int check = _accountDAO.CheckAcount(userName, password);
            if (check <= 0)
                return false;
            else
                return true;
        }
        //Insert
        public int InsertAccount(string userName, string passWord, int role, string address, string qualification, int age, string phoneNumber, string fullName)
        {
            return _accountDAO.InsertAccount(userName, passWord, role, address, qualification, age, phoneNumber, fullName);
        }
        //Update
        public int UpdateAccount(int accountID,string userName, int role, string address, string qualification, int age, string phoneNumber, string fullName)
        {
            return _accountDAO.UpdateAccount(accountID,userName,role, address, qualification, age, phoneNumber, fullName);    
        }
        //Check
        public bool CheckAccountExisted(string userName)
        {
            if (_accountDAO.CheckAccountExisted(userName) > 0)
                return true;
            else
                return false;
        }
        //Delete
        public int DeleteAccount(int accountID)
        {
            return _accountDAO.DeleteAccount(accountID);
        }
        //GetData
        public List<Account> GetAllAccount() 
        {
            List<Account> list = new List<Account>();
            DataTable dbTable = _accountDAO.GetAllAccount();
            foreach(DataRow dr in dbTable.Rows)
            {
                Account account = new Account();
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
                list.Add(account);
            }
            return list;
        }
        #region //Totalsearch
        //search account by username
        public List<Account> GetAllAccountByUserName(string userName)
        {
            List<Account> list = new List<Account>();
            DataTable dbTable = _accountDAO.GetDataByUserName(userName);
            foreach (DataRow dr in dbTable.Rows)
            {
                Account account = new Account();
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
                list.Add(account);
            }
            return list;
        }
        //search account by address
        public List<Account> GetAllAccountByAddress(string address)
        {
            List<Account> list = new List<Account>();
            DataTable dbTable = _accountDAO.GetDataByAddress(address);
            foreach (DataRow dr in dbTable.Rows)
            {
                Account account = new Account();
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
                list.Add(account);
            }
            return list;
        }
        //search account by qualification
        public List<Account> GetAllAccountByQualification(string qualification)
        {
            List<Account> list = new List<Account>();
            DataTable dbTable = _accountDAO.GetDataByQualification(qualification);
            foreach (DataRow dr in dbTable.Rows)
            {
                Account account = new Account();
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
                list.Add(account);
            }
            return list;
        }
        //search account by phonenumber
        public List<Account> GetAllAccountByPhoneNumber(string phoneNumber)
        {
            List<Account> list = new List<Account>();
            DataTable dbTable = _accountDAO.GetDataByPhoneNumber(phoneNumber);
            foreach (DataRow dr in dbTable.Rows)
            {
                Account account = new Account();
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
                list.Add(account);
            }
            return list;
        }
        //search account by fullname
        public List<Account> GetAllAccountByFullName(string fullName)
        {
            List<Account> list = new List<Account>();
            DataTable dbTable = _accountDAO.GetDataByFullName(fullName);
            foreach (DataRow dr in dbTable.Rows)
            {
                Account account = new Account();
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
                list.Add(account);
            }
            return list;
        }
        
        #endregion
        //GetData from Account by accountid
        public Account GetDataByAccountID(int accountID)
        {
            DataTable dbTable = _accountDAO.GetDataByID(accountID);
            Account account= new Account();
            foreach (DataRow dr in dbTable.Rows)
            {
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
            }
            return account;
        }
        //GetData from Account by username
        public Account GetDataByUserName(string userName)
        {
            DataTable dbTable = _accountDAO.GetProfileByUserName(userName);
            Account account = new Account();
            foreach (DataRow dr in dbTable.Rows)
            {
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
            }
            return account;
        }    
        //GetPass by username

        public Account GetPasswordByUserName(string userName)
        {
            DataTable dbTable = _accountDAO.GetPasswordByUserName(userName);
            Account account = new Account();
            foreach (DataRow dr in dbTable.Rows)
            {
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
            }
            return account;
        }

        //Get Role For UserName
        public Account GetRoleForUserName(string userName)
        {
            DataTable dbTable = _accountDAO.GetRoleForUserName(userName);
            Account account = new Account();
            foreach (DataRow dr in dbTable.Rows)
            {
                account.AccountID = int.Parse(dr["AccountID"].ToString());
                account.UserName = dr["UserName"].ToString();
                account.Password = dr["Password"].ToString();
                account.Role = int.Parse(dr["Role"].ToString());
                account.Address = dr["Address"].ToString();
                account.Qualification = dr["Qualification"].ToString();
                account.Age = int.Parse(dr["Age"].ToString());
                account.PhoneNumber = dr["PhoneNumber"].ToString();
                account.FullName = dr["FullName"].ToString();
            }
            return account;
        }
        //ChangPass
        public bool ChangePassword(string userName,string password)
        {
            if (_accountDAO.ChangePassword(userName, password) > 0)
                return true;
            else
                return false;
        }
    }
}
